package com.cssl.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cssl.pojo.SysStaff;
import com.cssl.serice.SystaffSerice;
import com.cssl.serice.impl.SystaffImpl;
import com.cssl.utli.ExcelUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

//员工  端口号：8001
@RestController
public class SysStaffController {

    @Resource
    private SystaffSerice systaff;

    @GetMapping("/login/{code}/{pwd}")
    public String login(@PathVariable("code")String code,@PathVariable("pwd") String pwd){
        System.out.println("code"+code);
        if (systaff.selectid(code,pwd) !=null){
            System.out.println(systaff.selectid(code,pwd));
            String json = "{\"msg\":\"true\",\"user\":" + JSON.toJSON(systaff.selectid(code,pwd)) + "}";
            return json;
        }
        return  "{\"msg\":\"false\"}";
    }


//    @GetMapping("/select/{id}")
//    public String select(@PathVariable("id") String id){
//        if (systaff.selectid2(id) !=null){
//            String json = "{\"user\":" + JSON.toJSON(systaff.selectid2(id)) + "}";
//            return json;
//        }
//        return  "{\"msg\":\"true\"}";
//    }

    @GetMapping("/update/{name}/{phone}/{address}/{id}")
    public String update(@PathVariable("name")String name,@PathVariable("phone") String phone,@PathVariable("address")String address,@PathVariable("id")String id){
        SysStaff s=new SysStaff();
        s.setAddress(address);
        s.setId(id);
        s.setName(name);
        s.setPhone(phone);
        if (systaff.updateById(s)){
            String json = "{\"msg\":\"true\",\"user\":" + JSON.toJSON(systaff.selectid2(id)) + "}";
           return json;
        }
        return "false";
    }

    @GetMapping("/updatePwd/{pwd}/{id}")
    public String update(@PathVariable("pwd")String pwd,@PathVariable("id")String id){
        SysStaff s=new SysStaff();
        s.setId(id);
        s.setPwd(pwd);
        if (systaff.updateById(s)){
            String json = "{\"msg\":\"true\",\"user\":" + JSON.toJSON(systaff.selectid2(id)) + "}";
            return json;
        }
        return "false";
    }

    @GetMapping("/age")
    public List<Integer> age(){
//        String json = "{\"boys\":" + JSON.toJSON(systaff.age()) +"\"girls\":" + JSON.toJSON(systaff.age2()) +"}";
//        return json;
        return systaff.age();
    }

    @GetMapping("/age2")
    public List<Integer> age2(){
        return systaff.age2();
    }



    //选择员工查询信息分页，还没写和前端连接的
    //选择员工查询
    ///page/{coding}/{name}/{dname}/{pageIndex}
    @RequestMapping("/page")
    public IPage<SysStaff> query(String coding, String name, String dname, Integer pageIndex, Integer pageSize) {
        System.out.println("name" + name);
        System.out.println("pageIndex" + pageIndex);
        IPage<SysStaff> iPage;
        if (pageIndex == null) {
            pageIndex = 1;
        }
        if (coding == "") {
            iPage = systaff.seleo(new Page<SysStaff>(pageIndex, pageSize), new QueryWrapper<SysStaff>()
                    .like(name != null, "s.name", name).like(coding !=null, "s.code", coding)
                    .like(dname != null, "d.dname", dname));
        }else {
            iPage = systaff.seleo(new Page<SysStaff>(pageIndex, pageSize), new QueryWrapper<SysStaff>()
                    .like(name != null, "s.name", name).eq(coding !=null, "s.code", coding)
                    .like(dname != null, "d.dname", dname));
        }

        return iPage;
    }

//    员工管理查询
    @RequestMapping("/querypage")
    public IPage<SysStaff> querys(String code,Integer dept_id,String updatetime,String status,Integer pageIndex, Integer pageSize){
        System.out.println("来了员工管理查询");
        IPage<SysStaff> iPage;
        if (pageIndex ==null){
            pageIndex =1;
        }
        if (updatetime.equals("1970-01-1")){
            updatetime ="";
        }
        if (code == ""){
            iPage = systaff.selecty(new Page<SysStaff>(pageIndex, pageSize), new QueryWrapper<SysStaff>()
                    .like(code !=null, "code", code).like(dept_id != null, "dept_id", dept_id)
                    .like(updatetime != null, "update_time", updatetime).like(status != null, "status", status)
                    .eq("is_deleted","0"));
        }else {
            iPage = systaff.selecty(new Page<SysStaff>(pageIndex, pageSize), new QueryWrapper<SysStaff>()
                    .eq(code != null, "code", code).like(dept_id != null, "dept_id", dept_id)
                    .like(updatetime != null, "update_time", updatetime).like(status != null, "status", status)
                    .eq("is_deleted","0"));
        }
        return iPage;
    }


    //添加员工 //修改
    @RequestMapping("/addsys")
    public boolean insertuser(Integer id,String code, String name, int gender, String phone, String address, int dept_id, String create_time) {
        SysStaff sysStaff = new SysStaff();
        System.out.println("添加"+id);
          sysStaff.setId(id);
          sysStaff.setCode(code);
          sysStaff.setName(name);
          sysStaff.setGender(gender);
          sysStaff.setPhone(phone);
          sysStaff.setAddress(address);
          sysStaff.setDept_id(dept_id);
          sysStaff.setCreate_time(create_time);
          return  systaff.saveOrUpdate(sysStaff);
    }

    //修改查询
    @GetMapping("/queryu")
    public SysStaff queryupdate(int id){
        return systaff.getById(id);
    }

    //修改
    @GetMapping("/Sysupdate")
    public boolean Sysupdate(int id,String code, String name, int gender, String avatar, Date birthday,
                              String phone,String address,String remark,int dept_id,int status,String create_time){
        SysStaff sysStaff=new SysStaff();
        sysStaff.setId(id);
        sysStaff.setCode(code);
        sysStaff.setName(name);
        sysStaff.setGender(gender);
        sysStaff.setAvatar(avatar);
        sysStaff.setBirthday(birthday);
        sysStaff.setPhone(phone);
        sysStaff.setAddress(address);
        sysStaff.setRemark(remark);
        sysStaff.setDept_id(dept_id);
        sysStaff.setStatus(status);
        sysStaff.setCreate_time(create_time);
        return systaff.saveOrUpdate(sysStaff);
    }

    //员工信息删除
    @GetMapping("/deleteuser")
    public boolean deleteuser(String code){
        return systaff.removeById(code);
    }





    //导入导出
    //导chu
    @RequestMapping("/dc")
   public String export(HttpServletResponse response){
        //导出查询
       List<SysStaff> list=systaff.queryUserInfo();
       //导出操作
       ExcelUtil.exportExcel(list,"用户信息","sheet1",SysStaff.class,"SysStaff.xls",response);
       return "成功";
   }

   @GetMapping("/rztj")
    public List<SysStaff> rztj(String month){
       if (month =="" || month==null){
           SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
           Date date = new Date();
           System.out.println(format.format(date));
           month=format.format(date);
       }
        return systaff.select(new QueryWrapper<SysStaff>().like("s.create_time",month));
   }

   @GetMapping("/queryNum")
    public List<Integer> query(){
        List<Integer> list=new ArrayList<>();
       list.add(systaff.querySum());
        list.add(systaff.querySy());
        list.add(systaff.queryZc());
        return list;
   }

   //解聘





    //入职表
    @RequestMapping("/Entry")
    public List<SysStaff> Entry(String create_time){
        System.out.println("入职传值"+create_time);
        return  systaff.Entry(create_time);
    }

    //在解聘添加的时候对员工表进行is_delete修改
    @RequestMapping("/supdate")
    public int supdate(int id){
        return systaff.supdate(id);
    }


}
